package com.magus.project.yddemo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.service.RoleService;
import com.magus.project.yddemo.entity.IndicatorRole;
import com.magus.project.yddemo.service.IndicatorRoleService;
import com.magus.sis.common.entity.ComUnit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 指标授权管理
 * 
 * @author Yingjian Wang
 * @date 2018-01-22 12:49
 */
@Api(tags = "IndicatorRoleManager", description = "指标授权管理")
@RestController
@RequestMapping("/yddemo/indicatorRoles")
@AppGroupInfo(key = "yddemo:indicatorRole", name = "指标授权管理", appKey = "yddemo")
public class IndicatorRoleController extends BaseController {

	@Autowired
	private IndicatorRoleService indicatorRoleService;

	@Autowired
	private RoleService roleService;

	/**
	 * 通过角色获取数据
	 * 
	 * @param role
	 *            角色
	 * @return IndicatorRole 对象
	 */
	@GetMapping
	@RequiresPermissions("yddemo:indicatorRole:view")
	@ApiOperation(value = "查询指标授权管理", notes = "通过角色指标授权管理")
	@AppFunctionInfo(key = "yddemo:indicatorRole:view", name = "指标授权", groupKey = "yddemo:indicatorRole", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorRoles/page/view")
	ResponseEntity<List<IndicatorRole>> findAll(@RequestParam String roleId,String dimension) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("role", roleService.findOne(roleId)));
		searchFilters.add(SearchFilter.equal("dimension", dimension));
		Specification<IndicatorRole> spec = buildFilterRequest(searchFilters);
		List<IndicatorRole> result = indicatorRoleService.findAll(spec);
		return ResponseEntity.ok(result);
	}

	/**
	 * 保存指标授权数据
	 * 
	 * @param indicatorRole
	 *            要保存的授权数据
	 * @param result
	 * @return
	 */
	@PutMapping
	@RequiresPermissions("yddemo:indicatorRole:save")
	@ApiOperation(value = "保存指标授权数据", notes = "保存指标授权数据")
	@AppFunctionInfo(key = "yddemo:indicatorRole:save", name = "保存指标授权数据", groupKey = "yddemo:indicatorRole", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorRoles/page/save")
	ResponseEntity<List<IndicatorRole>> add(@RequestBody List<IndicatorRole> indicatorRole, BindingResult result) {
		validate(indicatorRole, result);
		List<IndicatorRole> save = indicatorRoleService.save(indicatorRole);
		return ResponseEntity.status(HttpStatus.CREATED).body(save);
	}

	@GetMapping("/getUnits")
	@ApiOperation(value = "获取所有的电厂的机组", notes = "获取所有的电厂的机组")
	ResponseEntity<List<ComUnit>> getUnits() {
		return ResponseEntity.ok(indicatorRoleService.getAllUnit());
	}

}
